<template>
  <div class="session-list">
    <div class="session-list-wrap">
      <div class="session-head">
        <div class="session-title">历史对话</div>
        <el-icon class="expand-icon" @click="onFold"><SwitchFilled /></el-icon>
      </div>
      <div class="session-search">
        <el-input class="search-input" placeholder="搜素"></el-input>
      </div>
      <div class="list-wrap">
        <div class="group-wrap" v-for="g in list" :key="g.id">
          <div class="session-group">{{ g.name }}</div>
          <div
            class="session-item"
            v-for="item in g.children"
            :key="item.dialogId"
            :class="{ selected: item.selected }"
            @click="onSelect(g.id, item.dialogId)"
          >
            {{ item.question }}
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { getHistoryList } from '@/api/chartdb'
import { ref, defineProps, defineEmits, watch } from 'vue'
const emits = defineEmits(['hide', 'selectHistory'])
const list = ref([])

list.value = [
  {
    id: 1,
    name: '今天',
    children: [
      { name: '主业主营业务收入进度', id: 1, selected: false },
      { name: '什么是价格审批', id: 2, selected: false }
    ]
  }
]
const selectId = ref('')
const onSelect = (gid, dialogId) => {
  selectId.value = dialogId
  list.value.forEach((group) => {
    if (group.id == gid) {
      group.children.forEach((item) => {
        if (item.dialogId == dialogId) {
          item.selected = true
        } else {
          item.selected = false
        }
      })
    } else {
      group.children.forEach((item) => (item.selected = false))
    }
  })
}

watch(selectId, () => {
  emits('selectHistory', selectId.value)
})

const onFold = () => {
  emits('hide', false)
}
const params = {
  userName: '',
  userId: '',
  status: '',
  evaluateStatus: '',
  startIndex: 1,
  pageSize: 10,
  totalNum: 0
}

getHistoryList(params).then((res) => {
  list.value[0].children = res.data.data.records
})
</script>

<style lang="scss" scoped>
.session-list {
  position: relative;
}

.session-list-wrap {
  width: 220px;
  padding: 10px 0;
  font-size: 14px;
  .session-head {
    padding-left: 15px;
    padding-right: 10px;
    display: flex;
    .session-title {
      font-weight: 600;
      font-size: 16px;
    }
    .expand-icon {
      margin-left: auto;
      font-size: 20px;
      cursor: pointer;
    }
  }
  .session-search {
    padding-left: 15px;
    padding-right: 10px;
    width: 100%;
    margin-top: 10px;
    .search-input {
      width: 100%;
      height: 36px;
      line-height: 36px;
      background: rgba(226, 231, 245, 0.5);
      border-radius: 18px;
    }
  }
  .session-group {
    padding-left: 15px;
    padding-right: 10px;
    margin-top: 20px;
    color: #8590b3;
  }
  .session-item {
    height: 48px;
    line-height: 48px;
    border-radius: 0px 0px 0px 0px;
    border: 1px solid #f2f3f5;
    padding-left: 15px;
    padding-right: 10px;
    cursor: pointer;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .selected {
    background-color: #e2e7f5;
  }
}
</style>
